package com.baidu.location;

import android.content.Context;
import android.os.Build;
import android.os.Build.VERSION;
import android.os.Handler;
import android.os.Message;
import android.telephony.CellLocation;
import android.telephony.TelephonyManager;
import android.telephony.cdma.CdmaCellLocation;
import android.telephony.gsm.GsmCellLocation;
import java.lang.reflect.Method;
import java.util.LinkedList;
import java.util.List;

final class t
{
  private static String f = null;
  private static String g = null;
  private static Method k = null;
  private static Method l = null;
  private static Method m = null;
  private static Class n = null;
  private static long o = 3000L;
  private static int p = 3;
  private static boolean q = false;
  private final String a = "baidu_location_service";
  private Context b = null;
  private TelephonyManager c = null;
  private u d = new u(???);
  private List e = null;
  private Handler h = null;
  private v i = null;
  private boolean j = false;

  public t(Context paramContext, Handler paramHandler)
  {
    this.b = paramContext;
    this.h = paramHandler;
  }

  public static String a(boolean paramBoolean)
  {
    StringBuffer localStringBuffer = new StringBuffer(256);
    localStringBuffer.append("&sdk=");
    localStringBuffer.append(3.3F);
    if ((paramBoolean) && (ak.g.equals("all")))
      localStringBuffer.append("&addr=all");
    if (paramBoolean)
      localStringBuffer.append("&coor=gcj02");
    if (g == null)
    {
      localStringBuffer.append("&im=");
      localStringBuffer.append(f);
    }
    while (true)
    {
      localStringBuffer.append("&mb=");
      localStringBuffer.append(Build.MODEL);
      localStringBuffer.append("&resid=");
      localStringBuffer.append("12");
      localStringBuffer.append("&os=A");
      localStringBuffer.append(Build.VERSION.SDK);
      if (paramBoolean)
      {
        localStringBuffer.append("&sv=");
        String str2 = Build.VERSION.RELEASE;
        String str1 = str2;
        if (str2 != null)
        {
          str1 = str2;
          if (str2.length() > 5)
            str1 = str2.substring(0, 5);
        }
        localStringBuffer.append(str1);
      }
      return localStringBuffer.toString();
      localStringBuffer.append("&cu=");
      localStringBuffer.append(g);
    }
  }

  private void a(CellLocation paramCellLocation)
  {
    if ((paramCellLocation == null) || (this.c == null))
      return;
    if (!(q))
    {
      f = this.c.getDeviceId();
      q = f();
    }
    ak.e();
    u localu = new u(this);
    localu.e = System.currentTimeMillis();
    String str = this.c.getNetworkOperator();
    if ((str != null) && (str.length() > 0));
    while (true)
    {
      int i1;
      try
      {
        do
        {
          do
          {
            do
            {
              if (str.length() >= 3)
              {
                i2 = Integer.valueOf(str.substring(0, 3)).intValue();
                i1 = i2;
                if (i2 < 0)
                  i1 = this.d.c;
                localu.c = i1;
              }
              str = str.substring(3);
              if (str == null)
                break label675;
              char[] arrayOfChar = str.toCharArray();
              i1 = 0;
              while (true)
              {
                i2 = i1;
                if (i1 >= arrayOfChar.length)
                  break;
                i2 = i1;
                if (!(Character.isDigit(arrayOfChar[i1])))
                  break;
                i1 += 1;
              }
              i2 = Integer.valueOf(str.substring(0, i2)).intValue();
              i1 = i2;
              if (i2 < 0)
                i1 = this.d.d;
              localu.d = i1;
              if (!(paramCellLocation instanceof GsmCellLocation))
                break label432;
              localu.a = ((GsmCellLocation)paramCellLocation).getLac();
              localu.b = ((GsmCellLocation)paramCellLocation).getCid();
              localu.g = 'g';
            }
            while ((!(localu.b())) || ((this.d != null) && (this.d.a(localu))));
            this.d = localu;
            this.h.obtainMessage(31).sendToTarget();
            if (!(localu.b()))
              break label658;
            if (this.e == null)
              this.e = new LinkedList();
            i1 = this.e.size();
            if (i1 != 0)
              break label639;
            paramCellLocation = null;
          }
          while ((paramCellLocation != null) && (paramCellLocation.b == this.d.b) && (paramCellLocation.a == this.d.a));
          if (paramCellLocation != null)
            paramCellLocation.e = (this.d.e - paramCellLocation.e);
          this.e.add(this.d);
        }
        while (this.e.size() <= p);
        this.e.remove(0);
        label658: label432: label639: return;
      }
      catch (Exception localClass)
      {
        do
          while (true)
          {
            while (true)
            {
              do
              {
                do
                  while (true)
                    localException.printStackTrace();
                while (!(paramCellLocation instanceof CdmaCellLocation));
                localu.g = 'c';
              }
              while (Integer.parseInt(Build.VERSION.SDK) < 5);
              if (n == null);
              try
              {
                while (true)
                {
                  do
                  {
                    Class localClass = Class.forName("android.telephony.cdma.CdmaCellLocation");
                    n = localClass;
                    k = localClass.getMethod("getBaseStationId", new Class[0]);
                    l = n.getMethod("getNetworkId", new Class[0]);
                    m = n.getMethod("getSystemId", new Class[0]);
                  }
                  while ((n == null) || (!(n.isInstance(paramCellLocation))));
                  try
                  {
                    i2 = ((Integer)m.invoke(paramCellLocation, new Object[0])).intValue();
                    i1 = i2;
                    if (i2 < 0)
                      i1 = this.d.d;
                    localu.d = i1;
                    localu.b = ((Integer)k.invoke(paramCellLocation, new Object[0])).intValue();
                    localu.a = ((Integer)l.invoke(paramCellLocation, new Object[0])).intValue();
                  }
                  catch (Exception paramCellLocation)
                  {
                    paramCellLocation.printStackTrace();
                    return;
                  }
                }
              }
              catch (Exception paramCellLocation)
              {
                n = null;
                paramCellLocation.printStackTrace();
                return;
              }
            }
            paramCellLocation = (u)this.e.get(i1 - 1);
          }
        while (this.e == null);
        this.e.clear();
        return;
      }
      label675: int i2 = 0;
    }
  }

  private static boolean f()
  {
    if ((f == null) || (f.length() < 10))
      return false;
    try
    {
      char[] arrayOfChar = f.toCharArray();
      int i1 = 0;
      while (i1 < 10)
      {
        int i2;
        do
        {
          if (arrayOfChar[i1] <= '9');
          i2 = arrayOfChar[i1];
        }
        while (i2 < 48);
        i1 += 1;
      }
      return true;
    }
    catch (Exception localException)
    {
    }
    return false;
  }

  public final void a()
  {
    if (this.j == true);
    do
    {
      return;
      this.c = ((TelephonyManager)this.b.getSystemService("phone"));
      this.e = new LinkedList();
      this.i = new v(this);
    }
    while ((this.c == null) || (this.i == null));
    try
    {
      this.c.listen(this.i, 272);
      f = this.c.getDeviceId();
      label211: ak.f = "v3.3" + f + "|" + Build.MODEL;
    }
    catch (Exception localException3)
    {
      try
      {
        g = al.a(this.b);
        new StringBuilder("CUID:").append(g);
        ak.e();
      }
      catch (Exception localException3)
      {
        try
        {
          if (g != null)
            ak.f = "v3.3|" + g + "|" + Build.MODEL;
          new StringBuilder("CUID:").append(ak.f);
          ak.e();
          q = f();
          ak.a("baidu_location_service", "i:" + f);
          ak.e();
          this.j = true;
          return;
          localException1 = localException1;
          g = null;
        }
        catch (Exception localException3)
        {
          while (true)
          {
            break label211:
            localException3 = localException3;
          }
        }
      }
    }
  }

  public final void b()
  {
    if (!(this.j))
      return;
    if ((this.i != null) && (this.c != null))
      this.c.listen(this.i, 0);
    this.i = null;
    this.c = null;
    this.e.clear();
    this.e = null;
    ak.e();
    this.j = false;
  }

  public final int c()
  {
    return this.c.getNetworkType();
  }

  public final u d()
  {
    if ((((this.d == null) || (!(this.d.a())) || (!(this.d.b())))) && (this.c != null));
    try
    {
      a(this.c.getCellLocation());
      return this.d;
    }
    catch (Exception localException)
    {
    }
  }
}